package tuse.components.tuseViewer
{
	import UIFree.ObjectEvent;
	import UIFree.uif_ecomponent;
	import UIFree.uif_loader;
	import UIFree.uif_transition;
	
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.geom.Matrix3D;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	import tuse.classes.ts_events;
	import tuse.classes.ts_servers;

	public class ts_actor extends uif_ecomponent {
		private var _info			: Object;
		private var _contur			: GlowFilter;
		private var _couturValue	: Number;
		private var _couturId		: Number;
		
		private var _loader			: uif_loader;
		private var _bmd			: BitmapData;
		private var _rect			: Shape;
		private var _matrix			: Matrix3D;
		
		public static const	MODELSCALE: Number = 0.8; 
		
		public function ts_actor(layer : uif_ecomponent, actor_info: Object) {
			super(layer);
			init(actor_info);
			addEventListener(Event.ENTER_FRAME, doFrame);
		}
		
		protected function init(actor_info: Object): void {
			_info = actor_info;
			loadBitmap();
			transform.matrix3D = new Matrix3D();
			
			_contur = new GlowFilter(0xFFFFFF, 1, 20, 20, 2, 2);
			_couturValue 	= 0;
			_couturId		= 0;
		}
		
		protected function doFrame(e: Event): void {
			if (_couturValue > 0) {
				_contur.alpha = 0.5 + (Math.sin(_couturId) + 1) / 2 * _couturValue * 0.5;
				_loader.filters = [_contur];
				_couturId += 0.3;
			}
		}
		
		public function get loaderScale(): Number {
			return _loader.scaleX;
		}
		
		public function set loaderScale(a_lscale: Number): void {
			var mat: Matrix = new Matrix();
			mat.translate(-_loader.contentLoaderInfo.width / 2, -_loader.contentLoaderInfo.height / 2);
			mat.scale(a_lscale, a_lscale);
			mat.translate(_loader.contentLoaderInfo.width / 2, _loader.contentLoaderInfo.height / 2);
			_loader.transform.matrix = mat;
		}
		
		public function get info(): Object {
			return _info;
		}
		
		public function get contur(): Number {
			return _couturValue;
		}
		
		public function set contur(a_value: Number): void {
			if (a_value == 0) _loader.filters = [];
			if (_couturValue != a_value) _couturId = 0;
			_couturValue 	= a_value;
			fx('loaderScale', loaderScale, _couturValue?1.2:1, uif_transition.easeInOut(uif_transition.Back, 3)).start();
		}
		
		public function hitTest(p: Point): Boolean {
			if (_bmd) {
				var r: Vector3D  = display.view.toSurface(new Vector3D(x, y, z), p, 1/MODELSCALE);
/*				_rect.x = r.x;
				_rect.y = r.y;*/
				return _bmd.hitTest(new Point(0, 0), 0x80, new Point(r.x, r.y));
			}
			return false;
		}
		
		override public function dispose():void {
			_bmd.dispose();
			super.dispose();
		} 
		
		protected function doBitmapLoad(e: Event): void {
			_bmd = new BitmapData(_loader.contentLoaderInfo.width, _loader.contentLoaderInfo.height, true, 0x00FFFFFF);
			_bmd.draw(_loader);
			display.dispatchEvent(new ObjectEvent(ts_events.ACTORLOADCOMPLETE, this));
		}
		
		protected function loadBitmap(): void {
			_loader = new uif_loader(this, ts_servers.ACTORURLPATH + _info.image, doBitmapLoad);
		}
		
		public function get display(): ts_tuseDisplay {
			return parent.parent as ts_tuseDisplay;
		}
	}
}